<?php

namespace App\Admin\Controllers;

use DB;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\WxMessage;
use App\WxPublicUser;
use App\WxMsglog;

class WxmsgController extends Controller
{
	/**
	 * 
	 */
	public function index(Request $request)
	{
		$keywords = $request->input('keywords');
		$messages = WxMessage::whereIn('status', [0, 1]);
		if ($keywords) {
			$messages = $messages->where('raw_content', 'like', '%'.$keywords.'%');
		}
		$messages = $messages->paginate(15);
		$messages->appends([
				'keywords' => $keywords
			]);

		return view('admin.msg.index', compact('messages', 'keywords'));
	}

	/**
	 * 
	 */
	public function create()
	{
		return view('admin.msg.create');
	}

	/**
	 * 
	 */
	public function store(Request $request)
	{
		if ($request->input('type') == 1) {//循环推送
	        $validator = Validator::make($request->all(), [
				'content' => 'required',
	            'begin_time' => 'required|numeric|min:0|max:24',
				'end_time' => 'required|numeric|min:0|max:24',
				'interval' => 'required|numeric|min:15'
	        ], [
	        	'content.required' => '请设置消息内容',
	        	'begin_time.required' => '请设置开始发送时间',
	            'begin_time.numeric' => '发送时间只能为数字',
	            'begin_time.min' => '发送时间不早于0点',
	            'begin_time.max' => '发送时间晚于24点',
	        	'end_time.required' => '请设置结束发送时间',
	            'end_time.numeric' => '发送时间只能为数字',
	            'end_time.min' => '发送时间不早于0点',
	            'end_time.max' => '发送时间晚于24点',
	            'interval.required' => '请设置间隔时间',
	            'interval.numeric' => '间隔时间错误',
	            'interval.min' => '间隔时间不少于15分钟'
	        ]);
	        if ($request->input('begin_time') >= $request->input('end_time')) {
	            $validator->after(function($validator){
	                $validator->errors()->add('begin_time', '开始时间不晚于结束时间!');
	            });
	        } else {
	        	$diff = ($request->input('end_time') - $request->input('begin_time')) * 60;
	        	if ($request->input('interval') > $diff) {
	        		$validator->errors()->add('begin_time', '间隔时间过长!');
	        	}
	        }
		} elseif ($request->input('type') == 2) {//定时推送
	        $validator = Validator::make($request->all(), [
				'content' => 'required',
	            'crontab' => 'required|numeric|min:0|max:24'
	        ], [
	        	'content.required' => '请设置消息内容',
	        	'crontab.required' => '请设置定时时间',
	        	'crontab.numeric' => '定时时间错误',
	        	'crontab.min' => '定时时间错误',
	        	'crontab.max' => '定时时间错误'
	        ]);
		} else if ($request->input('type') == 3) {//关键字回复
	        $validator = Validator::make($request->all(), [
				'content' => 'required',
	            'keywords' => 'required'
	        ], [
	        	'content.required' => '请设置消息内容',
	        	'keywords.required' => '请设置回复关键字'
	        ]);
		} else {
			return;
		}
        if ($validator->passes()) {
        	$wx_msg = new WxMessage;
        	$wx_msg->content = base64_encode($request->input('content'));
        	$wx_msg->raw_content = $request->input('content');
        	$wx_msg->type = $request->input('type');
        	if ($request->input('begin_time')) {
        		$wx_msg->begin_time = $request->input('begin_time');
        	}
        	if ($request->input('end_time')) {
        		$wx_msg->end_time = $request->input('end_time');
        	}
        	if ($request->input('interval')) {
        		$wx_msg->interval = $request->input('interval');
        	}
        	if ($request->input('crontab')) {
        		$wx_msg->crontab = $request->input('crontab');
        	}
        	if ($request->input('keywords')) {
        		$wx_msg->keywords = $request->input('keywords');
        	}
        	$wx_msg->save();

            return ['flag' => true, 'msg' => '创建成功'];
        } else {
            return $validator->errors();
        }
	}

	/**
	 * 
	 */
	public function edit(WxMessage $message)
	{	
		return view('admin.msg.edit', compact('message'));
	}

	/**
	 * 
	 */
	public function restore(WxMessage $message, Request $request)
	{
		if ($message->type == 1) {//循环推送
	        $validator = Validator::make($request->all(), [
				'content' => 'required',
	            'begin_time' => 'required|numeric|min:0|max:24',
				'end_time' => 'required|numeric|min:0|max:24',
				'interval' => 'required|numeric|min:15'
	        ], [
	        	'content.required' => '请设置消息内容',
	        	'begin_time.required' => '请设置开始发送时间',
	            'begin_time.numeric' => '发送时间只能为数字',
	            'begin_time.min' => '发送时间不早于0点',
	            'begin_time.max' => '发送时间晚于24点',
	        	'end_time.required' => '请设置结束发送时间',
	            'end_time.numeric' => '发送时间只能为数字',
	            'end_time.min' => '发送时间不早于0点',
	            'end_time.max' => '发送时间晚于24点',
	            'interval.required' => '请设置间隔时间',
	            'interval.numeric' => '间隔时间错误',
	            'interval.min' => '间隔时间不少于15分钟'
	        ]);
	        if ($request->input('begin_time') >= $request->input('end_time')) {
	            $validator->after(function($validator){
	                $validator->errors()->add('begin_time', '开始时间不晚于结束时间!');
	            });
	        } else {
	        	$diff = ($request->input('end_time') - $request->input('begin_time')) * 60;
	        	if ($request->input('interval') > $diff) {
	        		$validator->errors()->add('begin_time', '间隔时间过长!');
	        	}
	        }
		} elseif ($message->type == 2) {//定时推送
	        $validator = Validator::make($request->all(), [
				'content' => 'required',
	            'crontab' => 'required|numeric|min:0|max:24'
	        ], [
	        	'content.required' => '请设置消息内容',
	        	'crontab.required' => '请设置定时时间',
	        	'crontab.numeric' => '定时时间错误',
	        	'crontab.min' => '定时时间错误',
	        	'crontab.max' => '定时时间错误'
	        ]);
		} else if ($message->type == 3) {//关键字回复
	        $validator = Validator::make($request->all(), [
				'content' => 'required',
	            'keywords' => 'required'
	        ], [
	        	'content.required' => '请设置消息内容',
	        	'keywords.required' => '请设置回复关键字'
	        ]);
		} else {
			return;
		}
        if ($validator->passes()) {
        	$message->content = base64_encode($request->input('content'));
        	$message->raw_content = $request->input('content');
        	if ($request->input('begin_time')) {
        		$message->begin_time = $request->input('begin_time');
        	}
        	if ($request->input('end_time')) {
        		$message->end_time = $request->input('end_time');
        	}
        	if ($request->input('interval')) {
        		$message->interval = $request->input('interval');
        	}
        	if ($request->input('crontab')) {
        		$message->crontab = $request->input('crontab');
        	}
        	if ($request->input('keywords')) {
        		$message->keywords = $request->input('keywords');
        	}
        	$message->save();

            return ['flag' => true, 'msg' => '编辑成功'];
        } else {
            return $validator->errors();
        }
	}

	/**
	 * 
	 */
	public function toggle(WxMessage $message, Request $request)
	{
		$message->status = $request->input('status');
		$message->save();
		return ['flag' => true];
	}

	/**
	 * 
	 */
	public function delete(WxMessage $message)
	{
		$message->status = 2;
		$message->save();
		return ['flag' => true];
	}

	/**
	 * 公众平台用户
	 */
	public function publicUser(Request $request)
	{
		$keywords = $request->input('keywords');
		$users = WxPublicUser::orderBy('updated_at', 'DESC');
		if ($keywords) {
			$users->where('nickname', 'like', '%'.$keywords.'%');
		}
		$count = $users->count();
		$users = $users->paginate(15);
		$users->appends([
				'keywords' => $keywords
			]);
		return view('admin.msg.pubuser', compact('users', 'keywords', 'count'));
	}

	/**
	 * 公众平台用户切换状态
	 */
	public function userToggle(WxPublicUser $user, Request $request)
	{
		$user->status = $request->input('status');
		$user->save();
		return ['flag' => true];
	}

	/**
	 * 用户消息日志
	 */
	public function msgLog(WxPublicUser $user, Request $request)
	{
		$now = $request->input('time') ? $request->input('time') : date('Y-m-d', time());
		$tomorrow = date('Y-m-d', strtotime("$now + 1 day"));
		$logs = WxMsglog::select(DB::raw('wx_message.raw_content, wx_message.type, wx_msglog.created_at'))->join('wx_message', 'wx_msglog.msg_id', '=', 'wx_message.id')->where(['wx_msglog.user_id' => $user->id])->whereBetween('wx_msglog.created_at', [$now, $tomorrow])->get();

		return view('admin.msg.log', compact('logs', 'user', 'now'));
	}

	/**
	 * [syncUser description]
	 * @return [type] [description]
	 */
	public function syncUser()
	{
		system('php /home/www/leyougame/artisan complete:user', $retval);
		if ($retval == 0) {
			return ['flag' => true];
		} else {
			return ['flag' => false];
		}
	}


}	